<?php
namespace WebAdmin\Controller;
use WebAdmin\Controller\BaseController;
class SettingController extends BaseController {

	//ThinkPHP的构造函数
	public function _initialize() {
		//自动加载的类
		parent::_initialize();
		new \Org\Admin_autoload();
	}
	
	public function home(){
		$Dao = D("Dic");
		if(!empty($_POST)){
		  if(!empty($_FILES['imgfile']['name'])){    		
    		 import ( '@.ORG.UploadFile' );
    		 $upload = new \UploadFile ();
    		 // 设置文件大小
    		 $upload->maxSize = 3292200;
    		 // 设置文件保存规则唯一
    		 $upload->saveRule = 'uniqid';
    		 // 设置上传文件的格式
    		 $upload->allowExts = array ();
    		 // 保存路径
    		 $root_path = dirname(dirname(dirname(dirname(__FILE__))));
    		 $upload->savePath = $root_path.'/www/Public/Uploads/';
    		 // 设置需要生成缩略图，仅对图像文件有效
    		 $upload->thumb = false;
    		
    		 // 上传失败返回错误信息
    		 if (! $upload->upload ()) {
    			$this->error ( $upload->getErrorMsg () );
    		 } else {
    			// $this->success('上传成功');
    			// 获取上传文件的信息
    			$inf = $upload->getUploadFileInfo ();
    		 }
    		}
    		if(!empty($inf)){
    			$inf[0]['savepath'] = str_replace($root_path."/www","",$inf[0]['savepath']);
    			$_POST['name'] = str_replace("./","/",$inf[0]['savepath']) . $inf [0] ['savename'];//"http://".FRONT_SITE_NAME.
    		}
    		$result = $Dao->where("id=".(int)$_POST['id'])->save(array('name'=>$_POST['name']));
    		//if($result){
    			header ( "Content-type: text/html; charset=utf-8" );
    			echo "<script>window.location.href='" . __APP__ . "/index.php/Setting/home/';</script>";
    			exit ();
    		//}
    		
		}else{
		  $item = $Dao->where("dickey='home-photo'")->find();
		  $this->assign('vo',$item);
		  $this->display();
		}
	}

	
	public function page(){
		 $Dao = D("Dic");
		 if(!empty($_POST)){
		  if(!empty($_FILES['imgfile']['name'])){    		
    		 import ( '@.ORG.UploadFile' );
    		 $upload = new \UploadFile ();
    		 // 设置文件大小
    		 $upload->maxSize = 3292200;
    		 // 设置文件保存规则唯一
    		 $upload->saveRule = 'uniqid';
    		 // 设置上传文件的格式
    		 $upload->allowExts = array ();
    		 // 保存路径
    		 $root_path = dirname(dirname(dirname(dirname(__FILE__))));
    		 $upload->savePath = $root_path.'/www/Public/Uploads/';
    		 // 设置需要生成缩略图，仅对图像文件有效
    		 $upload->thumb = false;
    		
    		 // 上传失败返回错误信息
    		 if (! $upload->upload ()) {
    			$this->error ( $upload->getErrorMsg () );
    		 } else {
    			// $this->success('上传成功');
    			// 获取上传文件的信息
    			$inf = $upload->getUploadFileInfo ();
    		 }
    		}
    		if(!empty($inf)){
    			$inf[0]['savepath'] = str_replace($root_path."/www","",$inf[0]['savepath']);
    			$_POST['name'] = str_replace("./","/",$inf[0]['savepath']) . $inf [0] ['savename'];//"http://".FRONT_SITE_NAME.
    		}
    		$result = $Dao->where("id=".(int)$_POST['id'])->save(array('name'=>$_POST['name']));
    		//if($result){
    			header ( "Content-type: text/html; charset=utf-8" );
    			echo "<script>window.location.href='" . __APP__ . "/index.php/Setting/page/';</script>";
    			exit ();
    	//	}
    		
		}else{
		  $item = $Dao->where("dickey='page-photo'")->find();
		  $this->assign('vo',$item);
		  $this->display();
		}
	}
  
    
}